package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ApArticleDto;
import com.heima.model.article.dtos.ApArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/3/14 10:06
 * @Version 1.0
 */
@Service
public class ApArticleServiceImpl implements ApArticleService {

    @Autowired
    private ApArticleMapper articleMapper;
    @Autowired
    private ApArticleConfigMapper configMapper;
    @Autowired
    private ApArticleContentMapper contentMapper;

    /**  每页默认显示条数 */
    public final static int DEFAULT_SIZE = 10;
    /**  每页最大显示条数 */
    public final static int MAX_SIZE = 50;

    @Override
    public ResponseResult load(ApArticleHomeDto dto, Integer loadType) {
        //处理页数
        Integer size = dto.getSize();
        if(size == null || size <= 0){
            size = DEFAULT_SIZE;
        }
        size = Math.min(size, MAX_SIZE);

        dto.setSize(size);

        if(dto.getMaxBehotTime() == null){
            dto.setMaxBehotTime(new Date());
        }
        if(dto.getMinBehotTime() == null){
            dto.setMinBehotTime(new Date());
        }

        return ResponseResult.okResult(articleMapper.loadArticles(dto,loadType));
    }

    @Override
    public ResponseResult saveArticle(ApArticleDto articleDto) {
        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(articleDto,article);
        article.setPublishTime(new Date());
        if(article.getId() == null){
            articleMapper.insert(article);
            //保存config
            ApArticleConfig config = new ApArticleConfig(article.getId());
            configMapper.insert(config);

            //保存内容
            ApArticleContent content = new ApArticleContent();
            content.setContent(articleDto.getContent());
            content.setArticleId(article.getId());
            contentMapper.insert(content);
        }else{
            articleMapper.updateById(article);

            ApArticleContent articleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, article.getId()));
            articleContent.setContent(articleDto.getContent());
            contentMapper.updateById(articleContent);
        }

        return ResponseResult.okResult(article.getId());
    }
}